<template>
  <div class="list">
    <div
      class="item"
      v-for="item in cities"
      :key="item"
      :ref="item"
      @click="handleClick"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >{{ item }}</div>
  </div>
</template>
<script>
export default {
  props: {
    cities: Array
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  data() {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    };
  },
  methods: {
    handleClick(e) {
      this.$emit("change", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timmer = setTimeout(() => {
          let touchY = e.touches[0].clientY - 89;
          let index = Math.floor((touchY - this.startY) / 22);
          if (index >= 0 && index < this.cities.length) {
            this.$emit("change", this.cities[index]);
          }
        }, 16);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.list {
  position: absolute;
  top: 1.78rem;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: $bgColor;

  .item {
    text-align: center;
    line-height: 0.44rem;
  }
}
</style>